import {queryCustomers, getCustomerById, approvalCustomer} from '../../services/customer/customer';
import {getAgencyagreementById, getAgencyagreementList} from '../../services/customer/agencyagreement';
import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');

export default {
    namespace: 'customerApproval',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        customerPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        agencyagreementPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        agencyagreementList: [],
        approvaList:[],
        dataSource: {},
        customer: {},
        agencyagreement: {},
        attachments:{},

    },
    reducers: {

        /**
         * 设置数据列表
         * @param state
         * @param payload
         * @returns {{list, page}}
         */
        setCustomerManageList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },


        setCustomer(state, action) {
            let payload = action.payload;
            return { ...state, dataSource: payload.dataSource,attachments:payload.attachments, approvaList: payload.approvaList
            };
        },

        /**
         * 设置协议数据列表
         * @param state
         * @param payload
         * @returns {{agencyagreementList: (Array|*), agencyagreementPage: *}}
         */
        setAgencyagreementList(state, { payload }) {
            return { ...state, agencyagreementList: payload.agencyagreementList, agencyagreementPage: payload.agencyagreementPage}
        },


        /**
         * 设置协议数据
         * @param state
         * @param action
         * @returns {{agencyagreement: string}}
         */
        setAgencyagreement(state, action) {
            let payload = action.payload;
            return {...state, agencyagreement: payload.agencyagreement,}
        },


    },
    effects: {
        /**
         * 分页查询客户
         *
         * @param call
         * @param put
         * @param select
         */
            *queryCustomerManageList({payload}, { call, put, select }) {

            let page = yield select(state => state.customerApproval.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(queryCustomers, {
                customerNO: R.isNil(payload) || R.isNil(payload.customerNO) ? "" : payload.customerNO,
                cstName: R.isNil(payload) || R.isNil(payload.cstName) ? "" : payload.cstName,
                cstType: R.isNil(payload) || R.isNil(payload.cstType) ? "" : payload.cstType,
                cstManage: R.isNil(payload) || R.isNil(payload.cstManage) ? "" : payload.cstManage,
                cstWaiter: R.isNil(payload) || R.isNil(payload.cstWaiter) ? "" : payload.cstWaiter,
                status: R.isNil(payload) || R.isNil(payload.status) ? "2" : payload.status,
                beginTime:R.isNil(payload)||R.isNil(payload.beginTime)?null:payload.beginTime,
                endTime:R.isNil(payload)||R.isNil(payload.endTime)?null:payload.endTime,
                page: page})

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                let listWithKey = R.isNil(content.list) ? [] : content.list;
                R.map((order) => {
                    order.key = order.id;
                }, listWithKey)
                yield put({
                    type: 'setCustomerManageList',
                    payload: {
                        list: listWithKey,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据id获取订单详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getCustomerById({ payload }, { call, put, select }) {
            const res = yield call(getCustomerById, { id: payload.id, isApproval: R.isNil(payload.isApproval) ? false : payload.isApproval });

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                let customer = data.customer;
                let attachments =data.attachments;
                let approvals = data.approvals;

                yield put({
                    type: "setCustomer",
                    payload: {
                        dataSource: customer,
                        attachments:attachments,
                        approvaList: approvals
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 获取客户代理协议表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getAgencyagreementById({ payload }, { call, put, select }) {
            const res = yield call(getAgencyagreementById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setAgencyagreement",
                    payload: {
                        agencyagreement: data,
                    }
                });
            }



        },


        /**
         * 获取客户代理协议表列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getAgencyagreementList({payload}, { call, put, select }) {

            let page = payload.page;
            const res = yield call(getAgencyagreementList, { id:payload.id, page });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setAgencyagreementList',
                    payload: {
                        agencyagreementList: R.isNil(content.list) ? [] : content.list,
                        agencyagreementPage : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 客户审核吧
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *approvalCustomer({ payload }, { call, put, select }) {
            const res = yield call(approvalCustomer, payload);
            if (res.data.resultCode === "ok") {
                message.info("审核成功！");

                if(payload.onSuccess)payload.onSuccess();
            
            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};